/**
 * 设备状态
 */
package com.zjzywl.smoke.repository;
import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.zjzywl.smoke.entity.DeviceStatus;


/**
 * @author Administrator
 *
 */
@Repository
public interface DeviceStatusRepository extends JpaRepository<DeviceStatus,Integer>,DeviceStatusCustomRepository {

	//删除同设备编号状态
	@Modifying
	@Transactional
	@Query("delete from DeviceStatus u where u.devCode=:devCode")
	void deleteByDevCode(@Param("devCode") String devCode);

	@Query("select count(u) from DeviceStatus u where u.status=:status")
	Integer countDeviceStatusByStatus(@Param("status") Integer status);

	@Query("select u from DeviceStatus u where u.status=:status")
	List<DeviceStatus> findByStatus(@Param("status") Integer status);
	
	@Query("select u from DeviceStatus u order by device.id")
	List<DeviceStatus> findOrderByDeviceCode();
	
	@Query("select u from DeviceStatus u where u.returnTime>=:returnTime")
	List<DeviceStatus> findByReturnTimeAfter(@Param("returnTime") Date returnTime);
	
	@Query("select u from DeviceStatus u where u.device.deviceType.deviceTypeId=:deviceTypeId")
	List<DeviceStatus> findByDeviceType(@Param("deviceTypeId") String deviceTypeId);
	
	@Query("select u from DeviceStatus u where u.status=:status and u.device.deviceType.deviceTypeId=:deviceTypeId ")
	List<DeviceStatus> findByStatusAndDeviceType(@Param("status") Integer status,@Param("deviceTypeId") String deviceTypeId);
	
	@Query("select count(u) from DeviceStatus u where u.status=:status and u.device.deviceType.deviceTypeId=:deviceTypeId")
	Integer countDeviceStatusByStatusAndDeviceType(@Param("status") Integer status,@Param("deviceTypeId") String deviceTypeId);

	DeviceStatus findByDevCode(String devCode);
	@Modifying
	@Transactional
	@Query("update DeviceStatus u set u.status=:status where u.devCode=:devCode")
	void updateStatus(@Param("devCode")String devCode, @Param("status")Integer status);
}
